home *** CD-ROM | disk | FTP | other *** search
- Path: news.lpr.carel.fi!usenet
- From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
- Newsgroups: comp.lang.c
- Subject: Re: Input into array of strings?
- Date: Fri, 08 Mar 1996 14:04:19 +0200
- Organization: Carelcomp Forest
- Message-ID: <31402243.A48@cmt.lpr.mail.carel.fi>
- References: <4hnv1e$o7n@rhea.glo.be>
- NNTP-Posting-Host: renoir.cclahti.carel.fi
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- Sven Claeys wrote:
- >
- > Hi,
- >
- > I've got a some kind of problem reading into arrays of strings. I'm a
- > bit rusty in C or C++, it's been a while, so please be patient with
- > me.
- >
- > here we go:
- >
- > i've got a structure
- >
- > struct ImmoData
- > {
- > char code[3][5];
- > ....
- > }....;
- >
- > now i want to read data from a file with fgets(...) into this array.
- >
- > I've tried
- > for (i=0; i<5; i++)
- > fgets(code[i],....);
- > or
- > for (i=0; i<5; i++)
- > fgets((char*) code[i], ...);
- >
- > but none of these seem to work. As i'm debugging the first code is
- > read correct, but the second is appended to the first and is also read
- > into the second, and so on.
- >
-
- You have declared a structure type that has member 'code' having three strings, their
- length being 5 bytes. If you really wanted to get five strings, then:
-
- struct ImmoData {
- char code[5][5]; /* five strings upto 5 bytes length */
- };
-
- would be a better solution. Also, your fgets(code ...) doesn't reference the member
- inside the struct. Try declaring a variable, eg:
-
- struct ImmoData var;
- ...
- fgets(var.code[i], ...);
-
- HTH,
- AriL
- --
- All my opinions are mine and mine alone.
-